<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2022-11-15 16:04:01
 * @Last Modified time: 2022-12-08 15:50:58
 */
namespace app\common\services\system;

use frank\base\BaseService;
use app\common\dao\system\ConfigDao;
use frank\exception\CommonException;
use frank\utils\helper\StringHelper;

/**
 * 系统配置项
 * Class ConfigService
 */
class ConfigService extends BaseService
{
	/**
     * ConfigService constructor.
     * @param ConfigDao $dao
     */
    public function __construct(ConfigDao $dao)
    {
        $this->dao = $dao;
    }

    public function getConfigDetail($filter)
    {
        $detail = $this->dao->detail($filter);
        if (!$detail) {
            throw new CommonException('数据不存在');
        }
        return $detail->toArray();
    }

    /**
     * 获取配置列表
     */
    public function getConfigList(array $where)
    {
        [$page, $limit] = $this->getPageValue();
        $list = $this->dao->getConfigList($where, $page, $limit);
        return $list;
    }

    public function getConfigItemsByTabId($tabId = 0){
        $list = $this->dao->getConfigSelect(['config_tab_id'=>$tabId]);
        $form = [];
        $rules = [];
        foreach ($list as $key => $item) {
            $form[$item['name_field']] = $item['value'] ?? '';
        }
        return ['form'=>$form,'list'=>$list];
    }   

    public function checkFieldRules($field,$value = ''){
        $detail = $this->getConfigDetail(['name_field'=>$field]);
        $rules = $this->itemRules($detail['required']);
        foreach ($rules as $key => $item) {
            switch ($key) {
                case 'required':
                    if (StringHelper::isTrue($item)  == 'true' && $value === '') {
                        throw new CommonException('请输入'.($detail['name'] ?? '') . '值');
                    }
                    break;
                case 'min':
                    if (empty($value) || $item > $value ) {
                        throw new CommonException(($detail['name'] ?? '') . '值不能小于'.$item);
                    }
                    break;
                case 'max':
                    if (empty($value) ||  $value > $item ) {
                        throw new CommonException(($detail['name'] ?? '') . '值不能大于'.$item);
                    }
                    break;
            }
        }
        return true;
    }

    public function itemRules($required,$se = ';'){
        $rules = [];
        if(!empty($required)){
            $rule = explode($se,$required);
            foreach ($rule as $k => $item) {
                if(strpos($item,',')){
                    $rules = $this->itemRules($item,',');
                }else{
                    $rulek = substr($item, 0, strrpos($item, ":"));
                    $str = substr($item, strripos($item, ":") + 1);
                    $rules = array_merge($rules,[$rulek=>is_numeric($str)?(int)$str:(string)$str]);
                }
            }
       }
       return $rules;
    }
}